<?php
namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\base\Exception;

/**
 * This is the model class for table "game".
 *
 * @property string $game_name
 * @property string $pinyin
 * @property string $pinyin_first_letter
 * @property integer $state_id
 * @property string $state_name
 * @property string $short_introduce
 * @property string $create_time
 * @property string $update_time
 * @property string $game_logo
 * @property string $game_downclick
 * @property string $game_visits
 * @property integer $isdel
 * @property integer $sort_num
 * @property integer $is_new
 * @property integer $is_hot
 * @property integer $is_comment
 * @property integer $score
 * @property integer $game_id
 * @property integer $search_comment
 * @property string $qqun
 * @property string $huodong
 * @property string $qqunlink
 * @property string $huodonglink
 */
class Game extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'game';
    }

    /*
     * 产生创建日期和更新日期时间戳
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'create_time',
                'updatedAtAttribute' => 'update_time'
            ]
        ];
    }

    /**
     * 成功保存游戏后，自动新增一条info信息
     */
    public function afterSave($insert = true, $changedAttributes)
    {
        if ($insert === true) {
            $tmp = GameInfo::findOne($this->game_id);
            // 如果已经存在，则忽略
            if (! is_object($tmp)) {
                $info = new GameInfo();
                $info->game_id = $this->game_id;
                $info->game_name = $this->game_name;
                $info->save();
            }
        }
        return TRUE;
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'game_name',
                    'pinyin',
                    'pinyin_first_letter',
                    'short_introduce'
                ],
                'required'
            ],
            [
                [
                    'state_id',
                    'create_time',
                    'update_time',
                    'game_downclick',
                    'game_visits',
                    'isdel',
                    'sort_num',
                    'is_new',
                    'is_hot',
                    'is_comment',
                    'score',
                    'game_id',
                    'search_comment'
                ],
                'integer'
            ],
            [
                [
                    'game_name'
                ],
                'string',
                'max' => 100
            ],
            [
                [
                    'score'
                ],
                'integer',
                'max' => 100,
                'min' => 1
            ],
            [
                [
                    'pinyin',
                    'state_name',
                    'game_logo'
                ],
                'string',
                'max' => 255
            ],
            [
                [
                    'pinyin_first_letter'
                ],
                'string',
                'max' => 1
            ],
            [
                [
                    'short_introduce'
                ],
                'string',
                'max' => 4000
            ],
            [
                [
                    'qqun',
                    'huodong',
                    'qqunlink',
                    'huodonglink'
                ],
                'string',
                'max' => 500
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'game_id' => '主键ID',
            'game_name' => '游戏名称',
            'pinyin' => '拼音',
            'pinyin_first_letter' => '拼音首字母',
            'state_id' => '状态ID',
            'state_name' => '状态名称',
            'short_introduce' => '简介',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'game_logo' => '游戏图标',
            'game_downclick' => '下载次数',
            'game_visits' => '访问次数',
            'isdel' => '是否删除',
            'sort_num' => '排序值',
            'is_new' => '是否新游',
            'is_hot' => '是否热门',
            'is_comment' => '是否推荐到猜你喜欢',
            'score' => '分数',
            'search_comment' => '是否在搜索中推荐',
            'qqun' => 'QQ群文字',
            'huodong' => '活动文字',
            'qqunlink' => 'QQ群链接',
            'huodonglink' => '活动链接'
        ];
    }

    // relationship
    /**
     * 游戏信息[一对一]
     */
    public function getInfo()
    {
        return $this->hasOne(GameInfo::className(), [
            'game_id' => 'game_id'
        ]);
    }

    /**
     * 游戏的Beta信息[一对多]
     */
    public function getBetas()
    {
        return $this->hasMany(GameBeta::className(), [
            'game_id' => 'game_id'
        ]);
    }

    /**
     * 游戏的Notice信息[一对多]
     */
    public function getNotices()
    {
        return $this->hasMany(GameNotice::className(), [
            'game_id' => 'game_id'
        ]);
    }

    /**
     * 游戏的version信息[一对多]
     */
    public function getVersions()
    {
        return $this->hasMany(GameVersion::className(), [
            'game_id' => 'game_id'
        ]);
    }

    /**
     * 对应的游戏状态[一个游戏仅能一个状态]
     */
    public function getState()
    {
        return $this->hasOne(GameState::className(), [
            'state_id' => 'state_id'
        ]);
    }

    /**
     * 对应的游戏类型[一个游戏可有多个类型]
     */
    public function getTypes()
    {
        return $this->hasMany(GameType::className(), [
            'type_id' => 'type_id'
        ])->viaTable('game_with_type', [
            'game_id' => 'game_id'
        ]);
    }
}
